<?php
class VehiculosAdminController extends AppController {

	var $name = 'VehiculosAdmin';
	var $uses = array('Vehiculo');
	var $helpers = array('Html', 'Form' );
	
	function beforeFilter() {		
		$this->checkSession();
		$this->MenuCreator->setModulo($this->name);
		$this->set('menu', $this->getMenuElements());
	}

	function index() {			
		$this->Vehiculo->recursive = 0;
		$this->set('vehiculos', $this->paginate());
	}

	function ver($id = null) {				
		if(!$id) {
			$this->Session->setFlash('Vehiculo Invalido');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->set('Vehiculo', $this->Vehiculo->read(null, $id));
	}

	function agregar() {
		if(!empty($this->data)) {
			$this->cleanUpFields();
			$this->Vehiculo->create();
			if($this->Vehiculo->save($this->data)) {
				$this->Session->setFlash('El Vehiculo ha sido agregado');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('El Vehiculo no pudo ser agregado. Por favor intenta de nuevo.');
			}
		}
	}

	function editar($id = null) {	
		if(!$id && empty($this->data)) {
			$this->Session->setFlash('Vehiculo Invalido');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Vehiculo->save($this->data)) {
				$this->Session->setFlash('El Vehiculo ha sido actualizado');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('El Vehiculo no pudo ser agregado. Por favor intenta de nuevo.');
			}
		}
		if(empty($this->data)) {
			$this->data = $this->Vehiculo->read(null, $id);
		}
	}

	function eliminar($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Vehiculo');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if($this->Vehiculo->del($id)) {
			$this->Session->setFlash('El Vehiculo #'.$id.' ');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}
}
?>